//
//  HMMainViewController.swift
//  Black22
//
//  Created by Apple on 16/11/18.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit
//  根视图控制器
class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //  设置自定义的tabbar
        
        let hmTabBar = HMTabBar()
        //  设置代理
        hmTabBar.hmDelegate = self
        
        //  使用self 就会有循环引用
        hmTabBar.callBack = { [weak self] in
            
            print("你点击了加号按钮")
            
            let comopseView = HMComposeView()
            
            comopseView.show(target: self!)
            
            
            
            
            
        }
        
        //  使用kvc方式设置tabbar的属性值
        setValue(hmTabBar, forKey: "tabBar")
        
        addChildViewController(childController: HMHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(childController: HMMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(childController: HMDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(childController: HMProfileTableViewController(), imageName: "tabbar_profile", title: "我的")
    }
    
    //  添加子视图控制器
    func addChildViewController(childController: UIViewController, imageName: String, title: String) {
        
        //  设置tabbar图片
        childController.tabBarItem.image = UIImage(named: imageName)
        //  修改图片的渲染方式
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //  设置tabbar文字颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //  设置字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 13)], for: .normal)
        
        //  设置导航栏的文字颜色
        UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont.systemFont(ofSize: 20), NSForegroundColorAttributeName: UIColor.orange]
        
        
        //  设置标题
//        childController.tabBarItem.title = title
//        childController.navigationItem.title = title

        //  代码合成一句
        childController.title = title
        //  创建导航控制器
        let nav = HMNavigationViewController(rootViewController: childController)
        
        //  添加子视图控制器
        addChildViewController(nav)
        
    }

}

// MARK:    -实现代理方法
extension HMMainViewController: HMTabBarDelegate {
    func didClickedComposeButton() {
        print("你点击了加号按钮")
    }
}







